{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'{\"orientation\":\"Up\",\"RequestId\":\"399b3314-2d47-4faf-9403-f16c062623c2\",\"image_size\":[1440,1080],\"lanFrom\":\"zh-CHS\",\"textAngle\":\"0.0\",\"errorCode\":\"0\",\"lanTo\":\"en\",\"exif\":\"Up\",\"resRegions\":[{\"boundingBox\":\"159,303,175,63\",\"color\":\"default\",\"linesCount\":1,\"lineheight\":60,\"context\":\"\\xe5\\xae\\x89\\xe5\\x85\\xa8\\xe4\\xbf\\xa1\\xe6\\x81\\xaf\",\"tranContent\":\"Safety information\",\"dir\":\"h\",\"lang\":\"zh-CHS\",\"colors\":[\"default\"]},{\"boundingBox\":\"201,379,662,109\",\"color\":\"default\",\"linesCount\":2,\"lineheight\":41,\"context\":\"\\xe4\\xbd\\xbf\\xe7\\x94\\xa8\\xe6\\x9c\\xaa\\xe7\\xbb\\x8f\\xe8\\xae\\xa4\\xe5\\x8f\\xaf\\xe6\\x88\\x96\\xe4\\xb8\\x8d\\xe5\\x85\\xbc\\xe5\\xae\\xb9\\xe7\\x9a\\x84\\xe7\\x94\\xb5\\xe6\\xba\\x90\\xe3\\x80\\x81\\xe5\\x85\\x85\\xe7\\x94\\xb5\\xe5\\x99\\xa8\\xe6\\x88\\x96\\xe7\\x94\\xb5\\xe6\\xb1\\xa0,\\xe5\\x8f\\xaf\\xe8\\x83\\xbd\\xe5\\xbc\\x95\\xe8\\xb5\\xb7\\xe7\\x81\\xab\\xe7\\x81\\xbe\\xe3\\x80\\x81\\xe7\\x88\\x86\\xe7\\x82\\xb8\\xe6\\x88\\x96\\xe5\\x85\\xb6\\xe5\\xae\\x83\\xe5\\x8d\\xb1\\xe9\\x99\\xa9\\xe3\\x80\\x82\",\"tranContent\":\"Use of unauthorized or incompatible power supplies, chargers or batteries may cause fire, explosion or other hazards.\",\"dir\":\"h\",\"lang\":\"zh-CHS\",\"colors\":[\"default\"]},{\"boundingBox\":\"193,516,670,143\",\"color\":\"default\",\"linesCount\":3,\"lineheight\":41,\"context\":\"\\xe8\\xaf\\xb7\\xe4\\xbd\\xbf\\xe7\\x94\\xa8\\xe6\\xa0\\x87\\xe5\\x87\\x86\\xe7\\x94\\xb5\\xe5\\x8e\\x8b5V,\\xe7\\x94\\xb5\\xe6\\xb5\\x812A\\xe7\\x9a\\x84\\xe9\\x80\\x82\\xe9\\x85\\x8d\\xe5\\x99\\xa8\\xe5\\x85\\x85\\xe7\\x94\\xb5\\xe3\\x80\\x82\\xe5\\xa6\\x82\\xe4\\xbd\\xbf\\xe7\\x94\\xa8\\xe5\\x85\\xb6\\xe5\\xae\\x83\\xe7\\xb1\\xbb\\xe5\\x9e\\x8b\\xe9\\x85\\x8d\\xe4\\xbb\\xb6,\\xe5\\x8f\\xaf\\xe8\\x83\\xbd\\xe8\\xbf\\x9d\\xe5\\x8f\\x8d\\xe6\\x9c\\xac\\xe8\\xae\\xbe\\xe5\\xa4\\x87\\xe7\\x9a\\x84\\xe4\\xbf\\x9d\\xe4\\xbf\\xae\\xe6\\x9d\\xa1\\xe6\\xac\\xbe\\xe4\\xbb\\xa5\\xe5\\x8f\\x8a\\xe6\\x9c\\xac\\xe8\\xae\\xbe\\xe5\\xa4\\x87\\xe6\\x89\\x80\\xe5\\xa4\\x84\\xe5\\x9b\\xbd\\xe5\\xae\\xb6\\xe7\\x9a\\x84\\xe7\\x9b\\xb8\\xe5\\x85\\xb3\\xe8\\xa7\\x84\\xe5\\xae\\x9a,\\xe5\\xb9\\xb6\\xe5\\x8f\\xaf\\xe8\\x83\\xbd\\xe5\\xaf\\xbc\\xe8\\x87\\xb4\\xe5\\xae\\x89\\xe5\\x85\\xa8\\xe4\\xba\\x8b\\xe6\\x95\\x85\\xe3\\x80\\x82\",\"tranContent\":\"Please use the adapter with standard voltage of 5V and current of 2A to charge. If other types of accessories are used, it may violate the warranty terms of the equipment and the relevant regulations of the country where the equipment is located, and may lead to safety accidents.\",\"dir\":\"h\",\"lang\":\"zh-CHS\",\"colors\":[\"default\"]},{\"boundingBox\":\"189,700,675,138\",\"color\":\"default\",\"linesCount\":3,\"lineheight\":41,\"context\":\"\\xe8\\xaf\\xb7\\xe5\\x9c\\xa8\\xe6\\xb8\\xa9\\xe5\\xba\\xa60\\xe2\\x84\\x83-35\\xe2\\x84\\x83\\xe8\\x8c\\x83\\xe5\\x9b\\xb4\\xe5\\x86\\x85\\xe4\\xbd\\xbf\\xe7\\x94\\xa8\\xe6\\x9c\\xac\\xe8\\xae\\xbe\\xe5\\xa4\\x87,\\xe5\\xb9\\xb6\\xe5\\x9c\\xa8\\xe6\\xb8\\xa9\\xe5\\xba\\xa6-20\\xe2\\x84\\x83C~+45\\'\\xe2\\x84\\x83\\xe8\\x8c\\x83\\xe5\\x9b\\xb4\\xe5\\x86\\x85\\xe5\\xad\\x98\\xe6\\x94\\xbe\\xe8\\xae\\xbe\\xe5\\xa4\\x87\\xe5\\x8f\\x8a\\xe9\\x85\\x8d\\xe4\\xbb\\xb6\\xe3\\x80\\x82\\xe5\\xbd\\x93\\xe7\\x8e\\xaf\\xe5\\xa2\\x83\\xe6\\xb8\\xa9\\xe5\\xba\\xa6\\xe8\\xbf\\x87\\xe9\\xab\\x98\\xe6\\x88\\x96\\xe8\\xbf\\x87\\xe4\\xbd\\x8e\\xe6\\x97\\xb6,\\xe5\\x8f\\xaf\\xe8\\x83\\xbd\\xe4\\xbc\\x9a\\xe5\\xbc\\x95\\xe8\\xb5\\xb7\\xe8\\xae\\xbe\\xe5\\xa4\\x87\\xe6\\x95\\x85\\xe9\\x9a\\x9c\\xe3\\x80\\x82\",\"tranContent\":\"Please use the equipment in the temperature range of 0\\xe2\\x84\\x83-35\\xe2\\x84\\x83, and store the equipment and accessories in the temperature range of -20\\xe2\\x84\\x83C~+45\\'\\xe2\\x84\\x83. When the ambient temperature is too high or too low, it may cause equipment failure.\",\"dir\":\"h\",\"lang\":\"zh-CHS\",\"colors\":[\"default\"]},{\"boundingBox\":\"188,883,676,90\",\"color\":\"default\",\"linesCount\":2,\"lineheight\":41,\"context\":\"\\xe8\\xaf\\xb7\\xe5\\x8b\\xbf\\xe5\\xb0\\x86\\xe8\\xae\\xbe\\xe5\\xa4\\x87\\xe6\\x9a\\xb4\\xe9\\x9c\\xb2\\xe5\\x9c\\xa8\\xe9\\xab\\x98\\xe6\\xb8\\xa9\\xe5\\xa4\\x84\\xe6\\x88\\x96\\xe5\\x8f\\x91\\xe7\\x83\\xad\\xe8\\xae\\xbe\\xe5\\xa4\\x87\\xe5\\x91\\xa8\\xe5\\x9b\\xb4,\\xe5\\xa6\\x82\\xe6\\x97\\xa5\\xe7\\x85\\xa7\\xe3\\x80\\x81\\xe5\\x8f\\x96\\xe6\\x9a\\x96\\xe5\\x99\\xa8\\xe3\\x80\\x81\\xe5\\xbe\\xae\\xe6\\xb3\\xa2\\xe7\\x82\\x89\\xe3\\x80\\x81\\xe7\\x83\\xa4\\xe7\\xae\\xb1\\xe6\\x88\\x96\\xe7\\x83\\xad\\xe6\\xb0\\xb4\\xe5\\x99\\xa8\\xe7\\xad\\x89\\xe3\\x80\\x82\",\"tranContent\":\"Do not expose the equipment to high temperatures or to heating equipment such as sunlight, heaters, microwaves, ovens or water heaters.\",\"dir\":\"h\",\"lang\":\"zh-CHS\",\"colors\":[\"default\"]},{\"boundingBox\":\"182,1020,692,96\",\"color\":\"default\",\"linesCount\":2,\"lineheight\":41,\"context\":\"\\xe5\\xbd\\x93\\xe5\\x85\\x85\\xe7\\x94\\xb5\\xe5\\xae\\x8c\\xe6\\xaf\\x95\\xe6\\x88\\x96\\xe8\\x80\\x85\\xe6\\x9c\\xaa\\xe5\\x85\\x85\\xe7\\x94\\xb5\\xe6\\x97\\xb6,\\xe8\\xaf\\xb7\\xe6\\x96\\xad\\xe5\\xbc\\x80\\xe5\\x85\\x85\\xe7\\x94\\xb5\\xe5\\x99\\xa8\\xe4\\xb8\\x8e\\xe8\\xae\\xbe\\xe5\\xa4\\x87\\xe7\\x9a\\x84\\xe8\\xbf\\x9e\\xe6\\x8e\\xa5\\xe5\\xb9\\xb6\\xe4\\xbb\\x8e\\xe7\\x94\\xb5\\xe6\\xba\\x90\\xe6\\x8f\\x92\\xe5\\xba\\xa7\\xe4\\xb8\\x8a\\xe6\\x8b\\x94\\xe6\\x8e\\x89\\xe5\\x85\\x85\\xe7\\x94\\xb5\\xe5\\x99\\xa8\\xe3\\x80\\x82\",\"tranContent\":\"When charging is complete or not, disconnect the charger from the device and unplug the charger from the power outlet.\",\"dir\":\"h\",\"lang\":\"zh-CHS\",\"colors\":[\"default\"]},{\"boundingBox\":\"180,1157,696,96\",\"color\":\"default\",\"linesCount\":2,\"lineheight\":46,\"context\":\"\\xe8\\xae\\xbe\\xe5\\xa4\\x87\\xe9\\x85\\x8d\\xe6\\x9c\\x89\\xe4\\xb8\\x8d\\xe5\\x8f\\xaf\\xe6\\x8b\\x86\\xe5\\x8d\\xb8\\xe7\\x9a\\x84\\xe5\\x86\\x85\\xe7\\xbd\\xae\\xe7\\x94\\xb5\\xe6\\xb1\\xa0,\\xe8\\xaf\\xb7\\xe5\\x8b\\xbf\\xe8\\x87\\xaa\\xe8\\xa1\\x8c\\xe6\\x9b\\xb4\\xe6\\x8d\\xa2\\xe7\\x94\\xb5\\xe6\\xb1\\xa0,\\xe4\\xbb\\xa5\\xe5\\x85\\x8d\\xe6\\x8d\\x9f\\xe5\\x9d\\x8f\\xe8\\xae\\xbe\\xe5\\xa4\\x87\\xe3\\x80\\x82\",\"tranContent\":\"The device is equipped with a non-removable built-in battery. Please do not replace the battery by yourself to avoid damaging the device.\",\"dir\":\"h\",\"lang\":\"zh-CHS\",\"colors\":[\"default\"]},{\"boundingBox\":\"180,1299,696,105\",\"color\":\"default\",\"linesCount\":2,\"lineheight\":46,\"context\":\"\\xe8\\xaf\\xb7\\xe6\\x8c\\x89\\xe7\\x85\\xa7\\xe5\\xbd\\x93\\xe5\\x9c\\xb0\\xe8\\xa7\\x84\\xe5\\xae\\x9a\\xe5\\xa4\\x84\\xe7\\x90\\x86\\xe6\\x9c\\xac\\xe8\\xae\\xbe\\xe5\\xa4\\x87\\xe3\\x80\\x81\\xe7\\x94\\xb5\\xe6\\xb1\\xa0\\xe5\\x8f\\x8a\\xe7\\x9b\\xb8\\xe5\\x85\\xb3\\xe9\\x85\\x8d\\xe4\\xbb\\xb6,\\xe4\\xb8\\x8d\\xe5\\x8f\\xaf\\xe5\\xb0\\x86\\xe5\\xae\\x83\\xe4\\xbb\\xac\\xe4\\xbd\\x9c\\xe4\\xb8\\xba\\xe7\\x94\\x9f\\xe6\\xb4\\xbb\\xe5\\x9e\\x83\\xe5\\x9c\\xbe\\xe5\\xa4\\x84\\xe7\\x90\\x86\\xe3\\x80\\x82\",\"tranContent\":\"Please dispose of the equipment, batteries and related accessories in accordance with local regulations. Do not treat them as domestic garbage.\",\"dir\":\"h\",\"lang\":\"zh-CHS\",\"colors\":[\"default\"]}]}'\n"
     ]
    }
   ],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "import sys\n",
    "import uuid\n",
    "import requests\n",
    "import base64\n",
    "import hashlib\n",
    "\n",
    "from imp import reload\n",
    "\n",
    "reload(sys)\n",
    "\n",
    "YOUDAO_URL = 'https://openapi.youdao.com/ocrtransapi'\n",
    "APP_KEY = '6fa280d0332fdbff'\n",
    "APP_SECRET = '0v0qAvcZezQXx6VMWjty8aimdKqZDpFF'\n",
    "\n",
    "\n",
    "def truncate(q):\n",
    "    if q is None:\n",
    "        return None\n",
    "    size = len(q)\n",
    "    return q if size <= 20 else q[0:10] + str(size) + q[size - 10:size]\n",
    "\n",
    "\n",
    "def encrypt(signStr):\n",
    "    hash_algorithm = hashlib.md5()\n",
    "    hash_algorithm.update(signStr.encode('utf-8'))\n",
    "    return hash_algorithm.hexdigest()\n",
    "\n",
    "\n",
    "def do_request(data):\n",
    "    headers = {'Content-Type': 'application/x-www-form-urlencoded'}\n",
    "    return requests.post(YOUDAO_URL, data=data, headers=headers)\n",
    "\n",
    "\n",
    "def connect():\n",
    "    f = open(r'D:\\2.jpg', 'rb')  # 二进制方式打开图文件\n",
    "    q = base64.b64encode(f.read()).decode('utf-8')  # 读取文件内容，转换为base64编码\n",
    "    f.close()\n",
    "\n",
    "    data = {}\n",
    "    data['from'] = 'zh-CHS'\n",
    "    data['to'] = 'en'\n",
    "    data['type'] = '1'\n",
    "    data['q'] = q\n",
    "    salt = str(uuid.uuid1())\n",
    "    signStr = APP_KEY + q + salt + APP_SECRET\n",
    "    sign = encrypt(signStr)\n",
    "    data['appKey'] = APP_KEY\n",
    "    data['salt'] = salt\n",
    "    data['sign'] = sign\n",
    "\n",
    "    response = do_request(data)\n",
    "    print(response.content)\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    connect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
